/*
 * BoxRemapper.java
 *
 *  Copyright (c) 2008 Corey Oliver and Anshul Kumaria
 *
 *  This file is part of Balls of Fury.
 *
 *  Balls of Fury is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Balls of Fury is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 *
 */

/*
 * EDIT:  03/13/2008 - This is an example of how an edit performed to the class 
 * would appear.
 */

package ballsoffury.playground;

import com.jme.math.Vector2f;
import com.jme.scene.shape.Box;
import java.nio.FloatBuffer;

/**
 * <code>BoxRemapper</code> allows for management of the game. Various methods 
 * are included in this class to show various screens, alter preferences,  
 * and exit the application.
 *
 * @author Corey Oliver
 * @version $Revision:$ $Date:$
 */
public class BoxRemapper {

    public static final int BACK = 1;
    public static final int RIGHT = 2;
    public static final int FRONT = 4;
    public static final int LEFT = 8;
    public static final int TOP = 16;
    public static final int BOTTOM = 32;
    public static final int ALL = 63;
    
    private float tex[] = new float[]{
        1,0, 0,0, 0,1, 1,1,
        1,0, 0,0, 0,1, 1,1,
        1,0, 0,0, 0,1, 1,1,
        1,0, 0,0, 0,1, 1,1,
        1,0, 0,0, 0,1, 1,1,
        1,0, 0,0, 0,1, 1,1
    }; 
    
    /**
     * Give the coordinates in the following order:
     * bottom right, bottom left, top left, top right.
     * For rotating or mirroring change the order.
     * The coordinates don't have to be rectangular, 
     * so you can create distortions.
     * @param side int the side that should be set, sides can be added together
     * @param coords the coordinates of the texture
     */
    public void setSide(int side, Vector2f... coords) {
        if (coords.length != 4) {
            throw new IllegalArgumentException("Requires 4 Vector2f objects.");
        }
        setSide(side, coords[0].x, coords[0].y, 
                      coords[1].x, coords[1].y, 
                      coords[2].x, coords[2].y, 
                      coords[3].x, coords[3].y);
    }
    
    /**
     * Give the coordinates in the following order:
     * bottom right, bottom left, top left, top right.
     * For rotating or mirroring change the order.
     * The coordinates don't have to be rectangular, 
     * so you can create distortions.
     * @param side the side that should be set, sides can be added together
     * @param coords the coordinates of the texture
     */
    public void setSide(int side, float... coords) {
        if (coords.length != 8) {
            throw new IllegalArgumentException("Requires 8 floats.");
        }        
        for(int i = 0; i < 6; i++) {
            int n = 1 << i;
            if ((side & n) > 0) {
                System.out.println("writing side " + i);
                for (int t = 0; t < 8; t++) {
                    tex[t + 8*i] = coords[t];
                }
            }
        }
    }
    
    /**
     * The Box to be remapped
     * @param box the box
     */
    public void remap(Box box) {
                FloatBuffer fb = box.getTextureBuffer(0, 0);
		fb.rewind();
		fb.put(tex);
    }

}